import request from "@/utils/request";
import type { MenuCommon } from "@/api/types/menuType";

// 获取所有菜单项
export type MenuData = {
    createdBy: string;
    createdTime: string;
    id: number;
    description: string;
    icon: string;
    href: string;
    name: string;
    level: number;
    operatorId: number | null;
    orderNum: number;
    parentId: number
    shown: boolean;
    updatedBy: string;
    updatedTime: string;
}

export function getMenuList() {
  return request<MenuCommon<MenuData[]>>({
    url: '/boss/menu/getAll',
    method: 'GET',
    })
}



// 更新或新增菜单项
// Pick提取出MenuData的部分属性，用于新增或更新菜单项
export type CreateOrEditMenuData = Pick<MenuData, 'parentId' | 'name' | 'icon' | 'href' | 'orderNum' | 'description' |'shown'>


export function updateOrAddMenu(menuInfo: CreateOrEditMenuData) {
     return request<MenuCommon<boolean>>({
      url: '/boss/menu/saveOrUpdate',
      method: 'POST',
      data: menuInfo
     })
} 




// 删除菜单
export function deleteMenu(id: string) {
  return request<MenuCommon<boolean>>({
    url: `/boss/menu/${id}`,
    method: 'DELETE'
  })
} 



// 编辑菜单
type SubMenuList = MenuData & { subMenuList: SubMenuList[] | null }
type EditMenuInfo = MenuCommon<{
  menuInfo: MenuData;
  parentMenuList: SubMenuList[];
}>

export function editMenu(id: string) {
  return request<EditMenuInfo>({
      url: '/boss/menu/getEditMenuInfo',
      method: 'GET',
      params: { id }
  })
}